﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using HighLowTriple7;
using PaymentBLL;

/// <summary>
/// Summary description for Game
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WSGame : System.Web.Services.WebService
{

    public WSGame()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(EnableSession = true)]
    public BetGameResult BetGame(int money, int typeBet)
    {
        var retVal1 = new BetGameResult()
        {
            Val1 = 7,
            Val2 = 7,
            Val3 = 7
        };
        if (typeBet != 1 && typeBet != 2)
        {
            retVal1.Result = 0;
            return retVal1;
        }
        if (!Utils.UserUtils.IsLogin)
        {
            retVal1.Result = -1;
            return retVal1;
        }

        int accountId = Utils.UserUtils.AccountId;
        string accessToken = Utils.UserUtils.AccessToken;

        var ctr = new CtrAccount();
        var infoAcc = ctr.SelectAccountInfo(accountId);
        if (typeBet == 1)
        {
            if (money > infoAcc.Au)
            {
                retVal1.Result = -2;
                return retVal1;
            }
        }
        if (typeBet == 2)
        {
            if (money > infoAcc.Ag)
            {
                retVal1.Result = -2;
                return retVal1;
            }
        }
        if (Triple7.GetGameStatus(accessToken) == 5)
        {
            retVal1.Result = -3;
            return retVal1;
        }
        int moneyStore = 0;

        if (typeBet == 1)
            Triple7.PlayerBetAu(money, accessToken);
        if (typeBet == 2)
            Triple7.PlayerBetAg(money, accessToken);

        if (Triple7.GetGameStatus(accessToken) == 4)
        {
            retVal1.Result = -3;
            return retVal1;
        }

        if (typeBet == 1)
            moneyStore = Triple7.GetAuCurrent(accessToken);
        if (typeBet == 2)
            moneyStore = Triple7.GetAgCurrent(accessToken);
        var retVal = new BetGameResult()
        {
            Val1 = Triple7.GetColumn1Value(accessToken),
            Val2 = Triple7.GetColumn2Value(accessToken),
            Val3 = Triple7.GetColumn3Value(accessToken),
            Result = 1,
            Money = moneyStore
        };
        return retVal;

    }
    [WebMethod(EnableSession = true)]
    public BetGameResult Choose(int col, int hi)
    {
        var retVal1 = new BetGameResult()
        {
            Val1 = 7,
            Val2 = 7,
            Val3 = 7
        };
        if (!Utils.UserUtils.IsLogin)
        {
            retVal1.Result = -1;
            return retVal1;
        }

        string accessToken = Utils.UserUtils.AccessToken;
        if (Triple7.GetGameStatus(accessToken) == 4)
        {
            retVal1.Result = -3;
            return retVal1;
        }
        int moneyStore = 0;


        if (hi > 0)
            Triple7.PlayerSelectHigh(col, accessToken);
        else
            Triple7.PlayerSelectLow(col, accessToken);

        int au = Triple7.GetAuCurrent(accessToken);
        int ag = Triple7.GetAgCurrent(accessToken);
        moneyStore = au > 0 ? au : ag;

        var retVal = new BetGameResult()
        {
            Val1 = Triple7.GetColumn1Value(accessToken),
            Val2 = Triple7.GetColumn2Value(accessToken),
            Val3 = Triple7.GetColumn3Value(accessToken),
            Result = Triple7.GetGameStatus(accessToken),
            Money = moneyStore
        };
        return retVal;
    }
    [WebMethod(EnableSession = true)]
    public int Collect()
    {

        if (!Utils.UserUtils.IsLogin)
        {
            return -1;
        }
        string accessToken = Utils.UserUtils.AccessToken;
        if (Triple7.GetGameStatus(accessToken) == 4)
        {
            return -3;
        }
        if (Triple7.GetRound(accessToken) < 1)
        {
            return -4;
        }
       
        Triple7.PlayerCollect(accessToken);
        if (Triple7.GetGameStatus(accessToken) == 4)
        {
            return 1;
        }

        return 0;
    }

}
public class BetGameResult
{
    public int Val1 { get; set; }
    public int Val2 { get; set; }
    public int Val3 { get; set; }
    public int Result { get; set; }
    public int Money { get; set; }
}
